/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.ucv.daos;

import pe.edu.ucv.entidades.Hardware;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author oguzman
 */
@Stateless
@LocalBean
public class HardwareDAO {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
      @PersistenceContext EntityManager em;
    public void crear(Hardware hardware){
        em.persist(hardware);
    }
    public void borrar(Integer id){
        Hardware hardware=em.find(Hardware.class, id);
        em.remove(hardware);
    }
    public List<Hardware> buscarTodas(){
        return em.createNamedQuery("Hardware.findAll",Hardware.class).getResultList();
    }
    public List<Hardware> buscarPornombre(String cadena){
        Query q=em.createNamedQuery("Hardware.findByDescripcionHardware",Hardware.class);
        q.setParameter("descripcionHardware", cadena);
        return q.getResultList();
                
        }   
    public void guardar(Hardware hardware){
        if(hardware.getId()==null){
            crear(hardware);
        }else{
            actualizar(hardware);
        }
    }
    public void actualizar(Hardware hardware){
      em.merge(hardware);
    }
}
